//
//  UserFeedBackController.m
//  CheekiOS
//
//  Created by mac on 2017/8/4.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserFeedBackController.h"
#import "HSLimitText.h"

@interface UserFeedBackController ()<HSLimitTextDelegate>
@property (nonatomic, strong) HSLimitText *FeenbackField;  ///< textField
@property (nonatomic, strong) UILabel *labTextFieldCount;
@property (nonatomic, strong) NSString *contentStr;

@end

@implementation UserFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavgationBarHiddenNo];
    
     _contentStr = @"";
    self.title = @"帮助与反馈";





    HSLimitText *textView = [[HSLimitText alloc] initWithFrame:CGRectMake(0, 0, 0, 0) type:TextInputTypeTextView];
    textView.placeholder = @"填写你需要帮助的内容或反馈内容！";
    textView.delegate = self;
    textView.maxLength = 120;
    
    textView.layer.shadowColor=[UIColor grayColor].CGColor;
    textView.layer.shadowOffset=CGSizeMake(1, 6);
    textView.layer.shadowOpacity=0.5;
    textView.layer.shadowRadius=8;
    [self.view addSubview:textView];


    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15*SCREEN_WIDTH_RATIO47);
        make.right.equalTo(self.view).offset(-15*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(self.view).offset(18.5*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(200*SCREEN_WIDTH_RATIO47);
    }];
    self.FeenbackField = textView;

    
    
    _labTextFieldCount =[UILabel new];
    _labTextFieldCount.text = @"您最多还能输入120个字";
    _labTextFieldCount.font = FontBold(15);
    _labTextFieldCount.textColor = RGB(175, 177, 180);
    _labTextFieldCount.textAlignment = NSTextAlignmentRight;
    
    [self.view addSubview:_labTextFieldCount];
    [_labTextFieldCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(KScreenWidth);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(_FeenbackField.mas_bottom).offset(30*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(40*SCREEN_WIDTH_RATIO47);
    }];



    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame =CGRectMake(25,266, KScreenWidth-50, 50);
    [nextButton setTitle:@"确定反馈" forState:UIControlStateNormal];
    nextButton.backgroundColor = Colorbackground;
    [nextButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(feedUpdate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];

    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(25*SCREEN_WIDTH_RATIO47);
        make.right.equalTo(self.view).offset(-25*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_labTextFieldCount.mas_bottom).offset(30*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(40*SCREEN_WIDTH_RATIO47);
    }];
    
    nextButton.layer.cornerRadius=(40/2)*SCREEN_WIDTH_RATIO47;

}
- (void)limitTextLimitInputOverStop:(HSLimitText *)textLimitInput
{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"超出字数" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:confirAction];
    [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    
}

- (void)limitTextLimitInput:(HSLimitText *)textLimitInput text:(NSString *)text
{
    _contentStr = text;
    
    
        if([textLimitInput isEqual:self.FeenbackField]){
            
            self.labTextFieldCount.text = [NSString stringWithFormat:@"您最多还能输入%d个字",(120-(int)text.length)];
        }
   
}


- (void)feedUpdate{

    
    if(_contentStr.length==0){
        [SVProgressHUD showErrorWithStatus:@"内容不能为空!"];
        return ;
    }
        NSDictionary *dict = [NSDictionary dictionary];
    
    
        dict = @{
                 @"uid":[HQHttpManager getUserId],
                 @"nickName":_contentStr
                 };
    
         [HcardHttpmanager getFeedBackUid:[HQHttpManager getUserId] content:_contentStr success:^(id result) {
             if ([result[@"code"] isEqualToString:@"ok"]) {
                 
                 [SVProgressHUD showSuccessWithStatus:@"反馈成功" ];
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     [SVProgressHUD dismiss];
                     
                 });
                 [self.navigationController popViewControllerAnimated:YES];

             }else{
                 
                     [SVProgressHUD showInfoWithStatus:result[@"message"]];
                 
                 }
         
         } failure:^(NSError *error) {
             
         }];
    
}


- (void)backActionbtn{

    [self.navigationController popViewControllerAnimated:YES];
}

@end
